package com.leetcode.tree.recursion;

import com.leetcode.basic.TreeNode;

/**
 * @author Dennis Li
 * @date 2020/6/15 16:18
 */
public class DiameterOfBinaryTree_543 {

    private int ans = 0;

    public int diameterOfBinaryTree(TreeNode root) {
        depth(root);
        return ans;
    }

    public int depth(TreeNode root) {
        if (root == null) return 0;
        int L = depth(root.left), R = depth(root.right);
        ans = Math.max(L + R, ans);
        return 1 + Math.max(L, R);
    }

}
